<?php

namespace Biz\model\GzDesignWeekActivity;

use Doraemon\pockets\datebase\BaseDbModel;

class GzDesignWeekActivityWinnerListModel extends BaseDbModel
{

    public const _CONFIG_FILE_ = 'db_store';


    public const _CONFIG_SELECT_ = 'db_brand_activity';

    public const _DATABASE_ = 'brand_activity';


    public const _TABLE_NAME = 'gz_design_week_activity_winner_list';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const TYPE_1 = 1;//中奖
    const Type_2 = 2;//未中奖

    const START_TIME =20230301; //活动开始时间
    const DAY_WINNER_LIMIT = [4,4,12,12,12,12,4]; //每日的抽奖名额限制


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }

    public static function selectByWhere($where, $field = '*'): array
    {
        return self::select($field, $where) ?? [];
    }

    public static function isWinnerByUid($uid)
    {
        $where['uid'] = $uid;
        $where['type'] = self::TYPE_1;
        $where['status'] = self::STATUS_NORMAL;

        return (int)self::count($where);
    }

    public static function toDayIsDrawByUid($uid)
    {
        $where['uid'] = $uid;
        $where['day'] = date('Ymd');
        $where['status'] = self::STATUS_NORMAL;

        return (int)self::count($where);
    }

    public static function getWinnerCount()
    {
        $where['day[<=]'] = date('Ymd');
        $where['day[>=]'] = self::START_TIME;
        $where['type'] = self::TYPE_1;
        $where['status'] = self::STATUS_NORMAL;

        return (int)self::count($where);
    }
    public static function getLastDayDrawCountByDay($day)
    {
        $where['day'] = $day;
        $where['status'] = self::STATUS_NORMAL;

        return (int)self::count($where);
    }

    public static function getWinnerLimit()
    {
        if((int)date('Ymd') >=self::START_TIME){
            $length = round((strtotime(date('Ymd'))- strtotime(self::START_TIME))/3600/24)+1;
        }else{
            $length=0;
        }
        
        return  array_sum(array_slice(self::DAY_WINNER_LIMIT,0, $length));
    }

}